package com.alvin.demo.job;

import org.quartz.*;

import java.util.concurrent.TimeUnit;

@DisallowConcurrentExecution
// @DisallowConcurrentExecution 防止并发执行任务, 无论任务耗时多少， 先执行玩此次任务，再执行下次任务
@PersistJobDataAfterExecution
// @PersistJobDataAfterExecution 使jobDataMap的数据持久化
public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDetail jobDetail = context.getJobDetail();

        System.out.println("任务名:: " + jobDetail.getKey().getName());
        System.out.println("组名:: " + jobDetail.getKey().getGroup());
        System.out.println("类名:: " + jobDetail.getKey().getClass());

        System.out.println("本次执行时间:: " + context.getFireTime());

        System.out.println("下次执行时间:: " + context.getNextFireTime());

        JobDataMap jobDataMap = jobDetail.getJobDataMap();

        int count = (int) jobDataMap.get("count");

        System.out.println("第 " + count + " 次执行任务");

        jobDataMap.put("count" , ++count);

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
